﻿using System;
using System.Configuration;
using System.Data.EntityClient;
using System.Data.Objects;

namespace RefLib.Data
{
    public sealed class ContextBuilder
    {
        public static T Build<T>(string connectionName) where T : ObjectContext
        {
            var dbConnection = ConfigurationManager.ConnectionStrings[connectionName];
            var ecsb = new EntityConnectionStringBuilder();
            ecsb.Provider = dbConnection.ProviderName;
            ecsb.ProviderConnectionString = dbConnection.ConnectionString;
            ecsb.Metadata = "res://*/";
            var entityConnection = new EntityConnection(ecsb.ToString());
            return (T) Activator.CreateInstance(typeof(T), new object[] { entityConnection });
        }

        private ContextBuilder() { }
    }
}
